﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eStock.hControl;
using HCommon;

namespace eStock
{
    public partial class frmCustomer_Add : hDialog
    {
        public frmCustomer_Add()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (btnSave.Error == true) { btnSave.Error = false; return; }
            SaveMaster();
        }

        private void SaveMaster()
        {
            try
            {
                int rs = 0;
                if (this.FormMode == Mode.Insert)
                {
                    rs = tblCustomer.Insert(txtCustomerCd.Text, txtCustomerName.Text, txtAddr.Text, txtPhoneNo.Text, txtFaxNo.Text, txtReceiver.Text, txtPosition.Text, txtHomePhone.Text, txtEmail.Text);
                }
                else
                {
                    rs = tblCustomer.Update(txtCustomerCd.Text, txtCustomerName.Text, txtAddr.Text, txtPhoneNo.Text, txtFaxNo.Text, txtReceiver.Text, txtPosition.Text, txtHomePhone.Text, txtEmail.Text);
                }

                Dialog.ShowMess(rs);

                this.UpdateKey = txtCustomerCd.Text;
                this.FormMode = Mode.Update;
                LoadMaster();
            }
            catch (Exception ex)
            {
                Common.GhiLog("SaveMaster", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void LoadMaster()
        {
            try
            {
                if (this.FormMode == Mode.Insert)
                {
                    txtCustomerCd.ReadOnly = false;
                    Common.ClearControl(grbCustomer);
                    return;
                }

                txtCustomerCd.ReadOnly = true;
                DataTable dt = new DataTable();
                dt = Dao.Get_Table("SELECT * FROM tblCustomer with(nolock) where CustomerCd = N'" + this.UpdateKey + "'");
                if (dt.Rows.Count == 0)
                {
                    Dialog.ShowMess(Dialog.mType.err, "Tài khoản này đã bị xóa!");
                    this.Close();
                    return;

                }
                txtCustomerCd.Text = dt.Rows[0]["CustomerCd"].ToString();
                txtCustomerName.Text = dt.Rows[0]["CustomerName"].ToString();
                txtAddr.Text = dt.Rows[0]["Addr"].ToString();
                txtPhoneNo.Text = dt.Rows[0]["PhoneNo"].ToString();
                txtFaxNo.Text = dt.Rows[0]["FaxNo"].ToString();

                txtReceiver.Text = dt.Rows[0]["Receiver"].ToString();
                txtPosition.Text = dt.Rows[0]["Position"].ToString();
                txtHomePhone.Text = dt.Rows[0]["HomePhone"].ToString();
                txtEmail.Text = dt.Rows[0]["Email"].ToString();
            }
            catch (Exception ex)
            {
                Common.GhiLog("LoadMaster", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void frmCustomer_Add_Load(object sender, EventArgs e)
        {
            LoadMaster();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.FormMode = Mode.Insert;
            LoadMaster();
        }
    }
}
